package com.mmcro.oauth.service;

import com.mmcro.oauth.entity.Client;
import com.mmcro.oauth.mapper.ClientMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 *  Oauth客户端服务
 *
 * @create: 朱志广
 **/
@Service
public class SysClientDetailService {

    @Autowired
    ClientMapper clientMapper;

    /**
     * 根据客户端id查询
     * @param clientId
     * @return org.springframework.security.oauth2.provider.client.BaseClientDetails
     */
    public BaseClientDetails selectById(String clientId) {



        Client client = clientMapper.findById(clientId);
        if (client==null){
            throw  new RuntimeException(clientId +" 不存在");
        }
        BaseClientDetails clientDetails = new BaseClientDetails();
        clientDetails.setAuthorities(new ArrayList<>());

        clientDetails.setClientId(client.getClientId());
        // 这个客户端秘钥和密码一样存BCryptPasswordEncoder加密后的接口，具体看定义的加密器
        clientDetails.setClientSecret(client.getClientSecret());

        // 设置accessToken和refreshToken的时效，如果不设置则使tokenServices的配置的
        clientDetails.setAccessTokenValiditySeconds((int) TimeUnit.HOURS.toSeconds(client.getAccessTokenValidity()));
        clientDetails.setRefreshTokenValiditySeconds((int)TimeUnit.DAYS.toSeconds(client.getRefreshTokenValidity()));

        // 资源id列表，需要注意的是这里配置的需要与ResourceServerConfig中配置的相匹配
        List<String> resourceIds = new ArrayList<>();
        String[] splitRes = client.getResourceIds().split(",");
         if(splitRes!=null){
             for (String resourceId:splitRes) {
                 resourceIds.add(resourceId);
             }
         }
        clientDetails.setResourceIds(resourceIds);

         // 设置scop
        List<String> scopes = new ArrayList<>(1);
        String[] splitScopes = client.getScope().split(",");
        for (String scop: splitScopes) {
            scopes.add(scop);
        }
        clientDetails.setScope(scopes);

        // 设置
        List<String> grantTypes = Arrays.asList(client.getAuthorizedGrantTypes().split(","));
       // List<String> grantTypes = new ArrayList<>(5);
      /*
        grantTypes.add("implicit");
        grantTypes.add("mobile");
        grantTypes.add("uimagecode");*/
        clientDetails.setAuthorizedGrantTypes(grantTypes);

        Set<String> sets = Arrays.asList(client.getWebServerRedirectUri().split(",")).stream().collect(Collectors.toSet());
      //  Set<String> sets = new HashSet<>(1);
       // sets.add("http://www.baidu.com");
        clientDetails.setRegisteredRedirectUri(sets);

        List<String> autoApproveScopes = Arrays.asList(client.getScope().split(","));
       // List<String> autoApproveScopes = new ArrayList<>(1);
        // autoApproveScopes.add("sever");
        // 自动批准作用于，授权码模式时使用，登录验证后直接返回code，不再需要下一步点击授权
        clientDetails.setAutoApproveScopes(autoApproveScopes);
        return clientDetails;
    }
}
